################################################################################################
# This script takes the QV/SV results & generates 2 histograms (Fig. 2 in Casella et al. (2010) 
# CU elections paper) for the subject strength of preferences. These are as follows: 
# 
# - The first calculation is the margin of victory for the number of votes (no BV or QV). The 
#   formula used is: (# votes in favor - # votes against)/Total Votes.  
# - The second calculation is the margin of victory of aggregate points (preferences).  The 
#   formula used is: (Sum points in favor - Sum points against)/Total Points allocaed.  
# - The last calculation is the margin of victory of average aggregate points (preferences). The 
#   formula is: (mean pts in favor - mean pts against)/(mean pts in favor + mean against). 
# 
# Exports the two histograms to the /California Stage 2 - Experiment Plots/Victory Margins folder. 
# 
# Author: Luis S.
# Last Modified: 7.24.16
################################################################################################

library(dplyr)
library(ggplot2)

# setwd("C:/Users/Luis/Dropbox/Research/Casella/Storable Votes (MTurk) - California Experiment/California Experiment Results/")

####################################
########### SV Histogram ###########
####################################

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean&Recoded_AllPos.csv", header = T, stringsAsFactors = F)
SV_summary <- read.csv("California Stage 2 - SV & QV Data/Summaries & Tests/SV_VoteSummary.csv", header = T, stringsAsFactors = F)


######################### SV Victory Margin - Simple Voting #########################

VM_SimpleVoting <- data.frame(Referenda = character(4), InFavor = numeric(4), Against = numeric(4), stringsAsFactors = F)
referendaList <- c("BilingualEduc_NoBV", "Immigration_NoBV", "PublicBonds_NoBV", "TeacherTenure_NoBV")

for(refNum in 1:length(referendaList))
{
  tempSummary <- SV_summary[SV_summary$Referenda == referendaList[refNum],]
  
  VM_SimpleVoting[refNum,] <- c(referendaList[refNum], tempSummary$InFavor[1], tempSummary$Against[1])
}

VM_SimpleVoting <- mutate(VM_SimpleVoting, InFavor = as.numeric(InFavor), Against = as.numeric(Against), 
                          VictoryMargin = (InFavor - Against)/(InFavor + Against), Referenda = gsub("_NoBV","", Referenda),
                          VictoryMargin = round(VictoryMargin, digits = 3)*100)


######################### SV Victory Margin - Mean Preference Intensity Scores #########################

VM_MeanPreferences <- data.frame(Referenda = c("BilingualEduc", "Immigration", "PublicBonds", "TeacherTenure"),
                                 InFavor = numeric(4), InFNum = numeric(4), Against = numeric(4), AgNum = numeric(4), 
                                 stringsAsFactors = F)

# Bilingual Educ
tempSummary <- group_by(SV_data, Ref1_BilingualEduc) %>% summarize(MeanPref = mean(EducationPref), Count = n()) %>% filter(MeanPref > 0)

VM_MeanPreferences$Against[1] <-  tempSummary$MeanPref[1]
VM_MeanPreferences$AgNum[1] <-  tempSummary$Count[1]
VM_MeanPreferences$InFavor[1] <-  tempSummary$MeanPref[2]
VM_MeanPreferences$InFNum[1] <-  tempSummary$Count[2]


# Immigration
tempSummary <- group_by(SV_data, Ref4_Immigration) %>% summarize(MeanPref = mean(ImmigrationPref), Count = n()) %>% filter(MeanPref > 0)

VM_MeanPreferences$Against[2] <-  tempSummary$MeanPref[1]
VM_MeanPreferences$AgNum[2] <-  tempSummary$Count[1]
VM_MeanPreferences$InFavor[2] <-  tempSummary$MeanPref[2]
VM_MeanPreferences$InFNum[2] <-  tempSummary$Count[2]

# Public Bonds
tempSummary <- group_by(SV_data, Ref3_PublicBonds) %>% summarize(MeanPref = mean(BondsPref), Count = n()) %>% filter(MeanPref > 0)

VM_MeanPreferences$Against[3] <-  tempSummary$MeanPref[1]
VM_MeanPreferences$AgNum[3] <-  tempSummary$Count[1]
VM_MeanPreferences$InFavor[3] <-  tempSummary$MeanPref[2]
VM_MeanPreferences$InFNum[3] <-  tempSummary$Count[2]

# Teachers Pref
tempSummary <- group_by(SV_data, Ref2_TeacherTenure) %>% summarize(MeanPref = mean(TeachersPref), Count = n()) %>% filter(MeanPref > 0)

VM_MeanPreferences$Against[4] <-  tempSummary$MeanPref[1]
VM_MeanPreferences$AgNum[4] <-  tempSummary$Count[1]
VM_MeanPreferences$InFavor[4] <-  tempSummary$MeanPref[2]
VM_MeanPreferences$InFNum[4] <-  tempSummary$Count[2]

VM_MeanPreferences <- mutate(VM_MeanPreferences, VictoryMargin = (InFavor - Against)/((InFavor*InFNum + Against*AgNum)/(InFNum + AgNum)),
                             VictoryMargin = round(VictoryMargin, digits = 3)*100)


######################### SV Victory Margin - Aggregate Preference Intensity Scores #########################

VM_AggregatePreferences <- mutate(VM_MeanPreferences, VictoryMargin = (InFavor*InFNum - Against*AgNum)/(InFavor*InFNum + Against*AgNum),
                                  VictoryMargin = round(VictoryMargin, digits = 3)*100)


######################### SV Histogram  ####################

VM_SimpleVoting <- select(VM_SimpleVoting, Referenda, VictoryMargin) %>% mutate(Type = "SimpleVoting")
VM_MeanPreferences <- select(VM_MeanPreferences, Referenda, VictoryMargin) %>% mutate(Type = "MeanPref")
VM_AggregatePreferences <- select(VM_AggregatePreferences, Referenda, VictoryMargin) %>% mutate(Type = "AggregatePref")

VM_combined <- bind_rows(VM_SimpleVoting, VM_MeanPreferences, VM_AggregatePreferences)

#plot
VMPlot <- ggplot(data = VM_combined, aes(x=Referenda, y = VictoryMargin, fill=Type)) + 
  geom_bar(stat="identity", position =  "dodge") + scale_y_continuous(limits = c(-25,40), breaks = seq(-30, 40, by = 5)) +  
  xlab("Referenda") + ylab("Victory Margin (%)") + ggtitle("SV - Victory Margins In Favor") +
  theme(axis.text.x=element_text(size=12, vjust = -.05), legend.text=element_text(size=16), title=element_text(size=14))
print(VMPlot)
ggsave(VMPlot, file="California Stage 2 - Experiment Plots/Victory Margins/SV - Victory Margins (In Favor).png", height = 4, width = 8.5)





####################################
######## QV Histograms (x4) ########
####################################

QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean&Recoded_AllPos.csv", header = T, stringsAsFactors = F)
QV_summary <- read.csv("California Stage 2 - SV & QV Data/Summaries & Tests/QV_VoteSummary.csv", header = T, stringsAsFactors = F)


######################### QV Victory Margin - Simple Voting #########################

VM_SimpleVoting <- data.frame(Referenda = character(4), InFavor = numeric(4), Against = numeric(4), stringsAsFactors = F)
referendaList <- c("BilingualEduc", "Immigration", "PublicBonds", "TeacherTenure")

for(refNum in 1:length(referendaList))
{
  tempSummary <- QV_summary[QV_summary$Referenda == referendaList[refNum],]
  
  VM_SimpleVoting[refNum,] <- c(referendaList[refNum], tempSummary$InFavor[1], tempSummary$Against[1])
}


VM_SimpleVoting <- mutate(VM_SimpleVoting, InFavor = as.numeric(InFavor), Against = as.numeric(Against), 
                          VictoryMargin = (InFavor - Against)/(InFavor + Against), VictoryMargin = round(VictoryMargin, digits = 3)*100)


######################### QV Victory Margin - Mean Preference Intensity Scores #########################

VM_MeanPreferences <- data.frame(Referenda = c("BilingualEduc", "Immigration", "PublicBonds", "TeacherTenure"),
                                 InFavor = numeric(4), InFNum = numeric(4), Against = numeric(4), AgNum = numeric(4), 
                                 stringsAsFactors = F)

# Bilingual Educ
tempSummary <- group_by(QV_data, Ref1_BilingualEduc) %>% summarize(MeanPref = mean(EducationPref), Count = n()) %>% filter(MeanPref > 0)

VM_MeanPreferences$Against[1] <-  tempSummary$MeanPref[1]
VM_MeanPreferences$AgNum[1] <-  tempSummary$Count[1]
VM_MeanPreferences$InFavor[1] <-  tempSummary$MeanPref[2]
VM_MeanPreferences$InFNum[1] <-  tempSummary$Count[2]


# Immigration
tempSummary <- group_by(QV_data, Ref4_Immigration) %>% summarize(MeanPref = mean(ImmigrationPref), Count = n()) %>% filter(MeanPref > 0)

VM_MeanPreferences$Against[2] <-  tempSummary$MeanPref[1]
VM_MeanPreferences$AgNum[2] <-  tempSummary$Count[1]
VM_MeanPreferences$InFavor[2] <-  tempSummary$MeanPref[2]
VM_MeanPreferences$InFNum[2] <-  tempSummary$Count[2]

# Public Bonds
tempSummary <- group_by(QV_data, Ref3_PublicBonds) %>% summarize(MeanPref = mean(BondsPref), Count = n()) %>% filter(MeanPref > 0)

VM_MeanPreferences$Against[3] <-  tempSummary$MeanPref[1]
VM_MeanPreferences$AgNum[3] <-  tempSummary$Count[1]
VM_MeanPreferences$InFavor[3] <-  tempSummary$MeanPref[2]
VM_MeanPreferences$InFNum[3] <-  tempSummary$Count[2]

# Teachers Pref
tempSummary <- group_by(QV_data, Ref2_TeacherTenure) %>% summarize(MeanPref = mean(TeachersPref), Count = n()) %>% filter(MeanPref > 0)

VM_MeanPreferences$Against[4] <-  tempSummary$MeanPref[1]
VM_MeanPreferences$AgNum[4] <-  tempSummary$Count[1]
VM_MeanPreferences$InFavor[4] <-  tempSummary$MeanPref[2]
VM_MeanPreferences$InFNum[4] <-  tempSummary$Count[2]

VM_MeanPreferences <- mutate(VM_MeanPreferences, VictoryMargin = (InFavor - Against)/((InFavor*InFNum + Against*AgNum)/(InFNum + AgNum)),
                             VictoryMargin = round(VictoryMargin, digits = 3)*100)



######################### QV Victory Margin - Aggregate Preference Intensity Scores #########################


VM_AggregatePreferences <- mutate(VM_MeanPreferences, VictoryMargin = (InFavor*InFNum - Against*AgNum)/(InFavor*InFNum + Against*AgNum),
                                  VictoryMargin = round(VictoryMargin, digits = 3)*100)



######################### QV Histogram  ####################

VM_SimpleVoting <- select(VM_SimpleVoting, Referenda, VictoryMargin) %>% mutate(Type = "SimpleVoting")
VM_MeanPreferences <- select(VM_MeanPreferences, Referenda, VictoryMargin) %>% mutate(Type = "MeanPref")
VM_AggregatePreferences <- select(VM_AggregatePreferences, Referenda, VictoryMargin) %>% mutate(Type = "AggregatePref")

VM_combined <- bind_rows(VM_SimpleVoting, VM_MeanPreferences, VM_AggregatePreferences)

#plot
VMPlot <- ggplot(data = VM_combined, aes(x=Referenda, y = VictoryMargin, fill=Type)) + 
  geom_bar(stat="identity", position =  "dodge") + scale_y_continuous(limits = c(-25,40), breaks = seq(-30, 40, by = 5)) +  
  xlab("Referenda") + ylab("Victory Margin (%)") + ggtitle("QV - Victory Margins In Favor") +
  theme(axis.text.x=element_text(size=12, vjust = -.05), legend.text=element_text(size=16), title=element_text(size=14))
print(VMPlot)
ggsave(VMPlot, file="California Stage 2 - Experiment Plots/Victory Margins/QV - Victory Margins (In Favor).png", height = 4, width = 8.5)
